Solution Explorer and Multi-forms

Objectives


Discussion

Solutions, Projects, and the Solution Explorer

Multiple Forms

Manipulating Forms

Me

Me.hide()

 Back to top


Demonstration

1.  Create a new project.

2.  Add a new form.  There are several ways to add a form.  One way is to select Project...Add Windows Form in the menu.  Another way is to right-click the project name in the Solution Explorer and select Add...Add Windows Form

3.  Run the project.  Which form is the startup form?  Unless you have changed the names of your forms, Form1 should appear when you run the project.  Now you will make Form2 the startup form.  Right-click the project in the Solution Explorer and choose Properties. In the dropdown box for the Startup Object select Form2.  Run your project.

4.  Now we will get the forms to interact.  Add a button to Form2: Name = "btnShowForm1"; text = "Show Form1". 

5.  In the click event for btnShowForm1 add the following highlighted code.  We will discuss the first two highlighted lines in a later unit in more detail.  Briefly these lines declare and create an object called frm1 which is an instance of Form1.

Private Sub btnShowForm1_Click... 
Dim frm1 As Form1 
frm1 = New Form1() 
frm1.Show()
End Sub

6.  Run your program. Notice that you can click back and forth between the forms.

7.  Now replace the line frm1.Show() with frm1.ShowDialog(). Run your program. Notice that you can not click back and forth between the forms.  The reason is that you used the ShowDialog method to display frm1 as a dialog form.  A dialog form requires the user to enter information and does not allow the user to return to what they were doing until they enter the information or close the form.

8.  Now we will make Form1 close itself.  Add a button to Form1:  Name = btnHideMe; Text = HideMe.  In the click event for btnHideMe add the following highlighted code:

Private Sub btnHideMe_Click(...
Me.Close()
End Sub

9.  Run the program and test the button.  Form1 should be closed when you click the button.  The keyword "Me" refers to the current instance of the object, in this case Form1.  The Close() method causes the form to be closed which means that it is hidden AND removed from memory. 

10. Replace the line Me.Close() with Me.Hide() and run the program.  Notice that it appears to run the same.  However, in this situation, while frm1 is being hidden it is still in memory.  This means that if there is any data on frm1 it will still be there even when it is hidden. We will explore this concept in a later lesson.

11. Now we will investigate the Load event for a form.  Whenever a form is loaded into memory, the Load event fires.  Double click on Form1. You should now be in the Load event for this form.  Note that you could also get to the event by selecting the code window and then choosing base class events in the left drop down and Load in the right drop down.  Type the following highlighted code in the Load event handler for Form1:

Private Sub Form1_Load(...
MessageBox.Show("Form1 loaded")
End Sub

12.  Run your program and test out the buttons.

13. Now make a similar messagebox appear when Form2 loads by adding the similar messagebox statement to the Load event for Form2.  Run your program and observe that this second messagebox appears when the program starts.  This is because Form2 is the startup form and is loaded when the program starts.

Back to top
Exercises

1.  Create a project that meets the following specifications:

a.  It has three forms (frmRed, frmBlue, and frmGreen).  Make the backcolor of each form correspond to the name of the form.  For example, frmRed should be red. 
b. frmGreen is the startup form.
c. It has two buttons on frmGreen. When the user clicks one of the buttons, frmRed should appear. When the user clicks the other button, frmBlue should appear.
d. It has buttons on frmRed and frmBlue to close the forms.
e. When each form loads, a messagebox should appear to indicate which form is loading.

2.  Add a button to one of the forms.  When the user clicks the button, the color of the form should change.

3. Add a textbox to one of the forms. When the user changes the size of the form, display the size  in the textbox.

4.  Explain how you make a form the startup form.

 

Back to top
Links & Help
Back to top